package com.yanze.service.impl;

import com.github.pagehelper.PageHelper;
import com.yanze.mapper.BookcommitMapper;
import com.yanze.mapper.BookimageMapper;
import com.yanze.mapper.BooksecondMapper;
import com.yanze.pojo.Bookcommit;
import com.yanze.pojo.Bookimage;
import com.yanze.pojo.Booksecond;
import com.yanze.service.BookSecondService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class BookSecondServiceimpl implements BookSecondService {

    @Autowired
    BooksecondMapper booksecondMapper;

    @Autowired
    BookimageMapper bookimageMapper;

    @Autowired
    BookcommitMapper bookcommitMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int addSecondBook(Booksecond bookBean) {
        int flag = booksecondMapper.insert(bookBean);
        return flag;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<Booksecond> getAllSecondBook(int page,int pageSize) {
        //开始分页
        PageHelper.startPage(page, pageSize);
        Example example = new Example(Booksecond.class);
        example.orderBy("time").desc();//按注册时间排序
        List<Booksecond> books = booksecondMapper.selectByExample(example);
        return books;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<Booksecond> getSecondBookById(String userid,int page,int pageSize) {
        //开始分页
        PageHelper.startPage(page, pageSize);
        Example example = new Example(Booksecond.class);
        Example.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmptyOrWhitespace(userid)) {
            criteria.andLike("userid", "%" + userid + "%");
        }
        example.orderBy("time").desc();//按发表时间排序
        List<Booksecond> books = booksecondMapper.selectByExample(example);
        return books;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int deleteSecondBook(Long bookid) {
        int flag = booksecondMapper.deleteByPrimaryKey(bookid);
        return flag;
    }

//    、、###################### 图片

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int insertImage(Bookimage imageBean) {
        int flag = bookimageMapper.insert(imageBean);
        return flag;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int insertImages(List<Bookimage> images) {
        int flag = bookimageMapper.insertList(images);
        return flag;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<Bookimage> queryImageByUid(Long bid) {
        Example example = new Example(Bookimage.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andLike("bookid", "%" + bid + "%");
        example.orderBy("time").desc();
        List<Bookimage> bookimages = bookimageMapper.selectByExample(example);
        return bookimages;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int addBookCommit(Bookcommit bookcommit) {
        int flag = bookcommitMapper.insert(bookcommit);
        return flag;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<Bookcommit> getAllCommit(String bookid) {
        Example example = new Example(Bookcommit.class);
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmptyOrWhitespace(bookid)) {
            criteria.andLike("bookid", "%" + bookid + "%");
        }
        example.orderBy("time").desc();
        List<Bookcommit> commits = bookcommitMapper.selectByExample(example);
        return commits;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int deleteCommit(Long commitid) {
        int flag = bookcommitMapper.deleteByPrimaryKey(commitid);
        return flag;
    }
}
